package com.elong.businesstravel.modules.member;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.AccountInfo;
import com.elong.businesstravel.entitys.RedPurse;
import com.elong.businesstravel.modules.member.adapter.MemberGetUseBonusAdapter;
import com.elong.businesstravel.modules.member.adapter.RedPurseAdapter;
import com.elong.businesstravel.protocol.login.MemberGetUseBonusRequest;
import com.elong.businesstravel.protocol.login.MemberGetUseBonusResponse;
import com.elong.businesstravel.protocol.login.MemberGetUseCouponsRequest;
import com.elong.businesstravel.protocol.login.MemberGetUseCouponsResponse;

/**
 * 我的红包
 * 
 * @author lenovo
 * 
 */
public class RedPurseActivity extends BaseActivity {
	
	private PullToRefreshListView mPullToRefreshListView;
	private RedPurseAdapter mRedPurseAdapter;

	private List<RedPurse> mRedPurses = new ArrayList<RedPurse>();
	private int mPage = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_red_purse);
		
		init();
	}
	
	@Override
	protected void init() {
		
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listView);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		mRedPurseAdapter = new RedPurseAdapter(mContext, mRedPurses);
		mPullToRefreshListView.setAdapter(mRedPurseAdapter);

		mPullToRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						// 更多
						memberGetUseBonus(true);

						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mPullToRefreshListView.onRefreshComplete();
							}
						});
					}
				});
		
		setTitleNavBar();
		
		memberGetUseBonus(false);
	}
	
	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("我的红包");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}
	
	/**
	 * 使用记录
	 */
	private void memberGetUseBonus(final boolean moreFlag) {
		
		if (moreFlag) {
			mPage++;
		} else {
			mPage = 1;
		}
		
		NetworkClient.getNetworkClient().PostRequest(
				new MemberGetUseCouponsRequest(mContext, mPage + ""),
				new MemberGetUseCouponsResponse(){
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								if (moreFlag) {
									// 更多
									if (0 == redPurses.size()) {
										HandlerToastUI.getHandlerToastUI(
												mContext, "没有更多信息");
									} else {
										mRedPurses.addAll(redPurses);
									}
								} else {
									// 刷新
									mRedPurses.clear();
									mRedPurses.addAll(redPurses);
								}
								mRedPurseAdapter.notifyDataSetChanged();
							}
						});
					}
				});
	}
	
}
